/**
 * 
 */
package sailing.filters.ui;

import java.awt.BorderLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.Border;

import sailing.filters.Filter;
import sailing.filters.FilterListModel;
import sailing.filters.FilterListModelEvent;
import sailing.filters.ui.laf.FilterListPanelUI;

import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

/**
 * @author kevin
 * 
 */
public class FilterListPanel extends JPanel implements FilterListPanelListener {

	private int cellCap = 0;
	private Insets margin = new Insets(2, 2, 2, 2);

	private JScrollPane scrollPane;
	private FilterListModel dataModel;
	private static Border contentBorder = BorderFactory.createEmptyBorder(2, 2, 2, 2);

	private static final String colSpec = "1dlu,f:150dlu:g,1dlu";

	private static String rowHeightDlu = "17dlu";
	private static String cellCapDlu = "1dlu";

	public FilterListPanel() {
		super();
		this.initial();
	}

	private void initial() {
		this.dataModel = createDefaultModel();
		this.setModel(dataModel);
	}

	private FilterListModel createDefaultModel() {
		return new FilterListModel();
	}

	public void setModel(FilterListModel dataModel) {
		if (dataModel == null) {
			throw new IllegalArgumentException("Cannot set a null TableModel");
		}

		if (this.dataModel != dataModel) {
			FilterListModel old = this.dataModel;
			if (old != null) {
				old.removeModelListener(this);
			}
			this.dataModel = dataModel;

			dataModel.addModelListener(this);

			dataChanged(new FilterListModelEvent(dataModel));

			firePropertyChange("model", old, dataModel);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see sailing.filters.FilterListPanelListener#dataChanged(sailing.filters.FilterListModelEvent)
	 */
	public void dataChanged(FilterListModelEvent filterListModelEvent) {
		// JPanel root = new FormDebugPanel(true, true);
		JPanel root = new JPanel();
		String rowSpec = this.getRowSpec(filterListModelEvent);
		FormLayout layout = new FormLayout(colSpec, rowSpec);
		root.setLayout(layout);
		CellConstraints cc = new CellConstraints();

		this.addFilterCellPanel(filterListModelEvent, root, cc);

		// Box root = Box.createVerticalBox();
		// root.setBorder(contentBorder);
		//
		// this.addFilterCellPanel(root);

		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			this.scrollPane.setUI(new FilterListPanelUI());
		}

		scrollPane.setViewportView(root);
		this.validate();
		this.setLayout(new BorderLayout());
		this.add(scrollPane, BorderLayout.CENTER);
	}

	private void addFilterCellPanel(FilterListModelEvent filterListModelEvent, JPanel root, CellConstraints cc) {
		final FilterListModel model = filterListModelEvent.getSource();
		List<Filter> filterList = model.getFilterList();
		int x = 2, y = 2;
		int size = filterList.size();
		for (int i = 0; i < size; i++) {
			final Filter f = filterList.get(i);
			root.add(f.getFilterCallPanel(), cc.xy(x, y));
			if (f.getRemoveBtn() != null) {
				f.getRemoveBtn().addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						model.remove(f);
					}
				});
			}
			y += 2;
		}
	}

	/**
	 * @param filterListModelEvent
	 * @return
	 */
	private String getRowSpec(FilterListModelEvent filterListModelEvent) {
		StringBuilder sb = new StringBuilder();
		sb.append("1dlu");
		FilterListModel model = filterListModelEvent.getSource();
		List<Filter> filterList = model.getFilterList();
		int size = filterList.size();
		for (int i = 0; i < size; i++) {
			sb.append(",");
			sb.append(rowHeightDlu);
			if (i < (size - 1)) {
				sb.append(",");
				sb.append(cellCapDlu);
			}
		}
		sb.append(",");
		sb.append("1dlu");
		return sb.toString();
	}

}
